//
//  LXRegisterViewController.swift
//  LXTXShop
//
//  Created by apple on 2018/4/9.
//  Copyright © 2018年 LX. All rights reserved.
//

import UIKit
import Extend
import SnapKit

class LXRegisterViewController: LXBaseViewController {

    let viewModel = LXRegisterViewModel()
    let usernameTextField = UITextField()
    let usernameLabel = UILabel()
    let passwordTextField = UITextField()
    let passwordLabel = UILabel()
    let repeatPasswordTextField = UITextField()
    let repeatPasswordLabel = UILabel()
    
    let registerButton = UIButton()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func createViews() {
        let superView = self.view!
        superView.backgroundColor = UIColor.white;
        
        let containerView = UIView()
        superView.addSubview(containerView)
        containerView.snp.makeConstraints { (ConstraintMaker) in
            ConstraintMaker.center.equalTo(superView)
            ConstraintMaker.left.equalTo(superView).offset(24)
        }
        
        usernameLabel.font = UIFont.systemFont(ofSize: 16)
        usernameLabel.textColor = UIColor.black
        containerView.addSubview(usernameLabel)
        usernameLabel.snp.makeConstraints { (ConstraintMaker) in
            ConstraintMaker.top.equalTo(containerView)
            ConstraintMaker.left.equalTo(containerView).offset(8)
        }
        usernameLabel.setContentHuggingPriority(UILayoutPriority(rawValue: 300), for: UILayoutConstraintAxis.horizontal)
        usernameLabel.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 800), for: UILayoutConstraintAxis.horizontal)
        
        usernameTextField.font = UIFont.systemFont(ofSize: 16)
        usernameTextField.textColor = UIColor.black
        usernameTextField.backgroundColor = UIColor.lightGray
        containerView.addSubview(usernameTextField)
        usernameTextField.snp.makeConstraints { (ConstraintMaker) in
            ConstraintMaker.top.equalTo(usernameLabel)
            ConstraintMaker.left.equalTo(usernameLabel.snp.right).offset(8)
            ConstraintMaker.right.equalTo(containerView)
        }
        
        passwordLabel.font = UIFont.systemFont(ofSize: 16)
        passwordLabel.textColor = UIColor.black
        containerView.addSubview(passwordLabel)
        passwordLabel.snp.makeConstraints { (ConstraintMaker) in
//            ConstraintMaker.top.greaterThanOrEqualTo(usernameLabel.snp.bottom).offset(8)
            ConstraintMaker.top.greaterThanOrEqualTo(usernameTextField.snp.bottom).offset(8)
            ConstraintMaker.left.equalTo(containerView).offset(8)
            ConstraintMaker.width.equalTo(usernameLabel)
        }
        passwordLabel.setContentHuggingPriority(UILayoutPriority(rawValue: 300), for: UILayoutConstraintAxis.horizontal)
        passwordLabel.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 800), for: UILayoutConstraintAxis.horizontal)
        
        passwordTextField.font = UIFont.systemFont(ofSize: 16)
        passwordTextField.textColor = UIColor.black
        passwordTextField.backgroundColor = UIColor.lightGray
        containerView.addSubview(passwordTextField)
        passwordTextField.snp.makeConstraints { (ConstraintMaker) in
            ConstraintMaker.top.equalTo(passwordLabel)
            ConstraintMaker.left.equalTo(passwordLabel.snp.right).offset(8)
            ConstraintMaker.right.equalTo(containerView)
        }
        
        repeatPasswordLabel.font = UIFont.systemFont(ofSize: 16)
        repeatPasswordLabel.textColor = UIColor.black
        containerView.addSubview(repeatPasswordLabel)
        repeatPasswordLabel.snp.makeConstraints { (ConstraintMaker) in
//            ConstraintMaker.top.greaterThanOrEqualTo(usernameLabel.snp.bottom).offset(8)
            ConstraintMaker.top.greaterThanOrEqualTo(passwordTextField.snp.bottom).offset(8)
            ConstraintMaker.left.equalTo(containerView).offset(8)
            ConstraintMaker.width.equalTo(repeatPasswordLabel)
        }
        repeatPasswordLabel.setContentHuggingPriority(UILayoutPriority(rawValue: 300), for: UILayoutConstraintAxis.horizontal)
        repeatPasswordLabel.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 800), for: UILayoutConstraintAxis.horizontal)
        
        repeatPasswordTextField.font = UIFont.systemFont(ofSize: 16)
        repeatPasswordTextField.textColor = UIColor.black
        repeatPasswordTextField.backgroundColor = UIColor.lightGray
        containerView.addSubview(repeatPasswordTextField)
        repeatPasswordTextField.snp.makeConstraints { (ConstraintMaker) in
            ConstraintMaker.top.equalTo(repeatPasswordLabel)
            ConstraintMaker.left.equalTo(repeatPasswordLabel.snp.right).offset(8)
            ConstraintMaker.right.equalTo(containerView)
        }
        
        registerButton.setTitleColor(UIColor.black, for: UIControlState.normal)
        containerView.addSubview(registerButton)
        registerButton.snp.makeConstraints { (ConstraintMaker) in
//            ConstraintMaker.top.greaterThanOrEqualTo(passwordLabel.snp.bottom).offset(24)
            ConstraintMaker.top.greaterThanOrEqualTo(repeatPasswordTextField.snp.bottom).offset(24)
            ConstraintMaker.centerX.equalToSuperview()
        }
        containerView.snp.makeConstraints { (ConstraintMaker) in
            ConstraintMaker.bottom.greaterThanOrEqualTo(registerButton)
        }
        
        usernameLabel.text = "用户名"
        passwordLabel.text = "密码"
        repeatPasswordLabel.text = "确认密码"
        usernameTextField.placeholder = "请输入用户名"
        passwordTextField.placeholder = "请输入密码"
        repeatPasswordTextField.placeholder = "请确认密码"
        passwordTextField.isSecureTextEntry = true
        repeatPasswordTextField.isSecureTextEntry = true
        registerButton.setTitle("注册", for: UIControlState.normal)
    }
    
    override func createEvents() {
        usernameTextField.rx.text.orEmpty
            .bind(to: viewModel.username).disposed(by: disposeBag)
        passwordTextField.rx.text.orEmpty
            .bind(to: viewModel.password).disposed(by: disposeBag)
        repeatPasswordTextField.rx.text.orEmpty
            .bind(to: viewModel.repeatPassword).disposed(by: disposeBag)
        registerButton.rx.tap
            .bind(to: viewModel.registerTaps).disposed(by: disposeBag)
        
        viewModel.usernameUsable
            .bind(to: usernameLabel.rx.validationResult).disposed(by: disposeBag)
        viewModel.usernameUsable
            .bind(to: passwordTextField.rx.inputEnabled).disposed(by: disposeBag)
        viewModel.passwordUsable
            .bind(to: passwordLabel.rx.validationResult).disposed(by: disposeBag)
        viewModel.passwordUsable
            .bind(to: repeatPasswordTextField.rx.inputEnabled).disposed(by: disposeBag)
        viewModel.repeatPasswordUsable
            .bind(to: repeatPasswordLabel.rx.validationResult).disposed(by: disposeBag)
        
        viewModel.registerButtonEnabled.subscribe(onNext:{
            [unowned self] valid in
            self.registerButton.isEnabled = valid
            self.registerButton.alpha = valid ? 1.0 : 0.5
        }).disposed(by: disposeBag)
        
        viewModel.registerResult.subscribe(onNext:{
            [unowned self] result in
            switch result{
            case let .ok(message):
                self.showAlert(message: message)
            case .empty:
                self.showAlert(message: "")
            case let .failed(message):
                self.showAlert(message: message)
            }
        }).disposed(by: disposeBag)
    }

    func showAlert(message: String) {
        let action = UIAlertAction(title: "确定", style: .default, handler: nil)
        let alertViewController = UIAlertController(title: nil, message: message, preferredStyle: .alert)
        alertViewController.addAction(action)
        present(alertViewController, animated: true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}
